package ch.zbw.pra.sysventory.business.businessObject.scan;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.EnumVariant;
import com.jacob.com.Variant;

import ch.zbw.pra.sysventory.model.Nic;

/**
 * Gibt alle Nic mittels WMI Schnittstelle auf einem bestimmten Host zurück
 * 
 * @author Ralph Koster
 */

public class ScanNicBusinessObject implements Serializable {

	private static final long serialVersionUID = 8227213713721091449L;
	private static Logger LOG = LoggerFactory.getLogger(ScanNicBusinessObject.class);
	
	private String nicName;
	private String nicIpv4;
	private String nicIpv6;
	private String nicMacAdress;
	private String nicStandardGateway;
	private boolean nicDhcp;
	private String nicSubnet;
	private List<Nic> nicInterfaces;

	public ScanNicBusinessObject() {
		this.nicInterfaces = new ArrayList<Nic>();
	}

	public List<Nic> getAllNicFromHost(String host) {
		String connectStr = String.format("winmgmts:\\\\%s\\root\\CIMV2", host);
		String query = "SELECT * FROM Win32_NetworkAdapterConfiguration";

		try {
			ActiveXComponent axWMI = new ActiveXComponent(connectStr);
			Variant vCollection = axWMI.invoke("ExecQuery", new Variant(query));

			EnumVariant enumVariant = new EnumVariant(vCollection.toDispatch());
			Dispatch item = null;
			while (enumVariant.hasMoreElements()) {
				item = enumVariant.nextElement().toDispatch();
				// Dispatch.call returns a Variant which we can convert to a java form.
				nicMacAdress = Dispatch.call(item, "MACAddress").toString();
				if (nicMacAdress != null && !"null".equals(nicMacAdress)) {
					nicName = Dispatch.call(item, "Description").toString();
					String ipAddress = Dispatch.call(item, "IPAddress").toString();
					if (ipAddress != null && !"null".equals(ipAddress)) {
						int index = ipAddress.lastIndexOf(' ');
						if (index == 0) {
							nicIpv4 = ipAddress.trim();
							nicIpv6 = "";
						} else {
							nicIpv4 = ipAddress.substring(0, index).trim();
							nicIpv6 = ipAddress.substring(index, ipAddress.length()).trim();
						}
					}
					nicStandardGateway = Dispatch.call(item, "DefaultIPGateway").toString().trim();
					if ("null".equals(nicStandardGateway)) {
						nicStandardGateway = "";
					}

					String dhcpNic = Dispatch.call(item, "DHCPEnabled").toString();

					nicSubnet = Dispatch.call(item, "IPSubnet").toString().trim();
					if (nicSubnet == "null") {
						nicSubnet = "";
					}

					LOG.debug("NIC found: " + nicName + " IPv4: " + nicIpv4 + " IPv6: " + nicMacAdress
							+ " MAC: " + nicMacAdress + " Gateway: " + nicStandardGateway
							+ " Subnet: " + nicSubnet);

					Nic nic = new Nic(nicName, nicIpv4, nicIpv6, nicMacAdress, nicStandardGateway,
							dhcpNic, nicSubnet);
					nicInterfaces.add(nic);
				}
			}
		} catch (Exception e) {
			LOG.debug("Failed to load nic on host: " + host);
			LOG.error(e.toString());
		}
		return nicInterfaces;
	}
}
